package cn.schoolwow.quickdao.module.common.operation.kit;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.database.parent.RealTimePrintStatementOption;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.domain.provider.DatabaseProvider;
import cn.schoolwow.quickdao.module.database.parent.kit.DatabaseDAOImpl;
import cn.schoolwow.quickflow.QuickFlow;

import javax.sql.DataSource;
import java.util.Map;
import java.util.stream.Collectors;

public class CommonDatabaseOptionImpl extends DatabaseDAOImpl implements CommonDatabaseOption {

    public CommonDatabaseOptionImpl(QuickFlow quickFlow) {
        super(quickFlow);
    }

    @Override
    public DataSource getDataSource() {
        return quickDAOConfig.databaseContext.dataSource;
    }

    @Override
    public Map<String, Entity> getEntityMap() {
        return quickDAOConfig.databaseContext.entityMap;
    }

    @Override
    public Entity getEntity(Class clazz) {
        return quickDAOConfig.databaseContext.entityMap.values().stream().filter(entity -> entity.clazz.getName().equalsIgnoreCase(clazz.getName())).findFirst().orElse(null);
    }

    @Override
    public Entity getEntity(String tableName) {
        return quickDAOConfig.databaseContext.entityMap.values().stream().filter(entity -> entity.tableName.equalsIgnoreCase(tableName)).findFirst().orElse(null);
    }

    @Override
    public Property getEntityProperty(Class clazz, String fieldName) {
        Entity entity = getEntity(clazz);
        if(null==entity){
            throw new IllegalArgumentException("未扫描到实体类!实体类名:"+clazz.getName());
        }
        return entity.getPropertyByFieldNameOrColumnName(fieldName);
    }

    @Override
    public DatabaseProvider getDatabaseProvider() {
        return quickDAOConfig.databaseContext.databaseProvider;
    }

    @Override
    public QuickDAOConfig getQuickDAOConfig() {
        return quickDAOConfig;
    }

    @Override
    public void recordFlowLog(boolean recordFlowLog) {
        quickDAOConfig.logRecordOption.recordFlowLog = recordFlowLog;
    }

    @Override
    public void recordSqlLog(boolean recordSqlLog) {
        quickDAOConfig.logRecordOption.recordSqlLog = recordSqlLog;
    }

    @Override
    public void startRecord() {
        quickDAOConfig.logRecordOption.record = true;
        StringBuffer stringBuffer = quickDAOConfig.logRecordOption.sqlRecordBuilderThreadLocal.get();
        if(null!=stringBuffer){
            stringBuffer.setLength(0);
        }
    }

    @Override
    public void addRecordString(String message) {
        ThreadLocal<StringBuffer> sqlRecordBuilderThreadLocal = quickDAOConfig.logRecordOption.sqlRecordBuilderThreadLocal;
        StringBuffer stringBuffer = sqlRecordBuilderThreadLocal.get();
        if(null==stringBuffer){
            sqlRecordBuilderThreadLocal.set(new StringBuffer());
        }
        stringBuffer = sqlRecordBuilderThreadLocal.get();
        stringBuffer.append(message+"\r\n");
    }

    @Override
    public String stopRecord() {
        quickDAOConfig.logRecordOption.record = false;
        StringBuffer stringBuffer = quickDAOConfig.logRecordOption.sqlRecordBuilderThreadLocal.get();
        if(null!=stringBuffer){
            return stringBuffer.toString();
        }else{
            return null;
        }
    }

    @Override
    public void realTimePrintStatement(RealTimePrintStatementOption realTimePrintStatementOption) {
        if(null==realTimePrintStatementOption){
            realTimePrintStatementOption = new RealTimePrintStatementOption();
        }
        if(null!=realTimePrintStatementOption.tableNameList){
            DatabaseProvider databaseProvider = quickDAOConfig.databaseContext.databaseProvider;
            realTimePrintStatementOption.tableNameList = realTimePrintStatementOption.tableNameList.stream().map(tableName -> databaseProvider.escape(tableName)).collect(Collectors.toList());
        }
        quickDAOConfig.logRecordOption.realTimePrintStatementOption = realTimePrintStatementOption;
    }

    @Override
    public void debug(boolean debug) {
        quickDAOConfig.logRecordOption.debug = debug;
    }
}
